package com.lgx.mobile.rest.group;

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lgx.common.exception.DataException;
import com.lgx.mobile.entity.Group;
import com.lgx.mobile.service.IGroupService;

/**
 * 
 * @author xuyichen
 *
 */
@Controller
@RequestMapping("/createGroup")
public class CreateGroupController {
	
	private static final Logger logger = Logger.getLogger(CreateGroupController.class);

	@Autowired
	IGroupService groupService;

	public IGroupService getGroupService() {
		return groupService;
	}

	/**
	 * 圈子创建
	 * @param name name is key
	 * @param createUserId createUserId is key
	 * @param categoryId categoryId is key
	 * @return 0：失败；1：成功；2：该用户今天已经创建了该类型的圈子；3：参数异常，有参数为空
	 */
	@RequestMapping(value="/{name}/{createUserId}/{categoryId}",method=RequestMethod.GET)
	@ResponseBody
	public Object getGroup(@PathVariable("name") String name,@PathVariable("createUserId") String createUserId,@PathVariable("categoryId") String categoryId){
		if(StringUtils.isNotBlank(name)&&StringUtils.isNotBlank(createUserId)&&StringUtils.isNotBlank(categoryId)){
			Group g = getGroupService().checkGroupExists(Long.parseLong(createUserId), new Date(), Long.parseLong(categoryId));
			if(g == null){
				String status = "0";
				Group group = new Group();
				group.setName(name);
				group.setCreateUserId(Long.parseLong(createUserId));
				group.setCreateDate(new Date());
				group.setCategoryId(Long.parseLong(categoryId));
				try {
					getGroupService().createGroup(group);
					status = "1";
				} catch (DataException e) {
					e.printStackTrace();
				}
				return status;
			}else{
				if(logger.isDebugEnabled()){
					logger.debug("createGroup faild name and createUserId and categoryId is error");
				}
//				"createGroup faild group is exists";
				return "2";
			}
		}else{
			if(logger.isDebugEnabled()){
				logger.debug("createGroup faild name and createUserId and categoryId is not filled");
			}
//		    "createGroup faild please check name and createUserId and categoryId is filled";
			return "3";
		}
	}
	
	/**
	 * 圈子创建
	 * @param name name is key
	 * * @param description description is key
	 * @param createUserId createUserId is key
	 * @param categoryId categoryId is key
	 * @return 0：失败；1：成功；2：该用户今天已经创建了该类型的圈子；3：参数异常，有参数为空
	 */
	@RequestMapping(value="/{name}/{description}/{createUserId}/{categoryId}",method=RequestMethod.GET)
	@ResponseBody
	public Object getGroup(@PathVariable("name") String name,@PathVariable("description") String description,
							@PathVariable("createUserId") String createUserId,@PathVariable("categoryId") String categoryId){
		if(StringUtils.isNotBlank(name)&&StringUtils.isNotBlank(description)
				&&StringUtils.isNotBlank(createUserId)&&StringUtils.isNotBlank(categoryId)){
			Group g = getGroupService().checkGroupExists(Long.parseLong(createUserId), new Date(), Long.parseLong(categoryId));
			if(g == null){
				String status = "0";
				Group group = new Group();
				group.setName(name);
				group.setDescription(description);
				group.setCreateUserId(Long.parseLong(createUserId));
				group.setCreateDate(new Date());
				group.setCategoryId(Long.parseLong(categoryId));
				try {
					getGroupService().createGroup(group);
					status = "1";
				} catch (DataException e) {
					e.printStackTrace();
				}
				return status;
			}else{
				if(logger.isDebugEnabled()){
					logger.debug("createGroup faild name and createUserId and categoryId is error");
				}
//				"createGroup faild group is exists";
				return "2";
			}
		}else{
			if(logger.isDebugEnabled()){
				logger.debug("createGroup faild name and createUserId and categoryId is not filled");
			}
//		    "createGroup faild please check name and createUserId and categoryId is filled";
			return "3";
		}
	}
}
